621. 任务调度器
为保证权益,题目请参考 621. 任务调度器(From LeetCode).
解决方案1
CPP
C++
#include <iostream>
#include <vector>
#include <algorithm>
#include <stack>
using namespace std;
class Solution {
public:
int leastInterval(vector<char> &tasks, int n) {
struct taskNode {
char ch;
int times;
taskNode() {
times = 0;
}
};
vector<taskNode> tasksVec(26, taskNode());
for (int i = 0; i < 26; i++) {
tasksVec[i].ch = char('A' + i);
}
for (char task: tasks) {
tasksVec[int(task) - 'A'].times += 1;
}
sort(tasksVec.begin(), tasksVec.end(), [&](taskNode &a, taskNode &b) -> bool {
return a.times > b.times;
});
//show
for (int i = 0; i < 26; i++) {
cout << tasksVec[i].ch << ", " << tasksVec[i].times << endl;
}
return 0;
}
};
int main() {
Solution so;
vector<char> vec;
vec.push_back('A');
vec.push_back('D');
vec.push_back('C');
vec.push_back('D');
cout << so.leastInterval(vec, 2) << endl;
return 0;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57